package com.fzu.ess.ess.service.Impl;

import com.fzu.ess.common.Const;
import com.fzu.ess.common.Model.JsonModel;
import com.fzu.ess.ess.dao.ReportDao;
import com.fzu.ess.ess.entity.Book;
import com.fzu.ess.ess.entity.Report;
import com.fzu.ess.ess.service.ReportService;
import com.fzu.ess.sys.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by ercha on 2015/10/25.
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;

    @Override
    public JsonModel create(Report report, String bookId) {
        JsonModel j = new JsonModel();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        User user = (User)session.getAttribute(Const.SESSION.USER);
        try {
            if(bookId==null || bookId.length()<1){
                return j;
            }
//            System.out.println("userId: " + user.getId());
            report.setInformant(user);
            report.setBook(new Book(bookId));
            report.setStatus(0);
            j.setSuccess(null != reportDao.save(report));
        } catch (Exception e){
            e.printStackTrace();
        }
        return j;
    }
}
